define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var CONST = require('oa/act/js/actcont');
	var dgutil,datagridconfig;


	var addStatus = {0:'未导入',1:'已导入'};
	var conf = {
		columns:[[
		         {field:'businessId',title:'流程编码',width:100,align:'center',height:50,hidden:true},
		         {field:'iconUrl',title:'流程图片',width:100,align:'center',height:50,formatter:function(value,row,index){
						if (value){
							return "<img class='click' style='width:30px;height:30px;' src='"+BASEPATH+value+"'/>";
						}
		        	}},
		         {field:'businessName',title:'流程名称',width:100,align:'center',height:50,formatter:function(value,row,index){
						if (value){
							return value;
						}
		        	}},
		         {field:'type',title:'流程类型',width:100,align:'center',height:50,formatter:function(val,row,i){
		        		 return '<span style="color:#c3c0c0">预设模板</span>';
		         }},
		         {field:'add_status',title:'导入状态',width:100,align:'center',height:50,formatter:function(val,row,i){
		        	 if(val == 0){
		        		 return '<span style="color:#c3c0c0">'+addStatus[val]+'</span>'
		        	 }else{
		        		 return '<span style="color:green">'+addStatus[val]+'</span>'
		        	 }
	         	 }}
			    ]]
	}
	
	var addDialogInit = function(baseUrl,row){
		if(row.add_status=="1"){
			$.messager.show({
				title:'系统消息',
				msg:'模板已被导入,不能重复导入!',
				timeout:5000,
				showType:'slide'
			});
			return;
		}else{
		$("#adddlg").dialog({
			iconCls:'icon-save',
			title:'一键导入',
			href:baseUrl,
			onLoad:function(){
				using(["combobox"], function() {
					var sysTypeData=[];
					ajax.init().success(function(code,msg,data){
						if(code==0){
							for(var k in data){
								sysTypeData.push({
									'groupId': data[k].groupId,
									'groupName': data[k].groupName,
								});
							}
							$('#groupName').combobox({    
							    valueField:'groupId',    
							    textField:'groupName',
							    data:sysTypeData
							});  
							$('#groupName').combobox('select', sysTypeData[0].groupId);

						}else{
							$.messager.show({
								title:'系统消息',
								msg:msg,
								timeout:5000,
								showType:'slide'
							});
						}
		    		}).post(BASEPATH+'/oa/act/processGroup/queryGroupByDept.htm');
				})
			},
			buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addform").form("validate")){
							ajax.init().success(function(code,msg,data){
								if(code==0){
									var param ={
											groupId:$("#addform").serializeJson().groupName,
											checkeds:[row.processKey]
									};
									ajax.init().success(function(code,msg,data){
										if(code==0){
											dgutil.pager();
											$.messager.show({
												title:'系统消息',
												msg:'模板已被导入',
												timeout:5000,
												showType:'slide'
											});
											$("#adddlg").dialog("close");
										}else{
											$.messager.show({
												title:'系统消息',
												msg:msg,
												timeout:5000,
												showType:'slide'
											});
										}
									}).sync().contentType('application/json').post(BASEPATH+'/oa/act/processGroup/addProcessToGroup.htm',JSON.stringify(param));
								}else{
									$.messager.show({
										title:'系统消息',
										msg:msg,
										timeout:5000,
										showType:'slide'
									});
								}
							}).post(BASEPATH+"/oa/act/approval/progress/addProcessToCompany.htm",{key:row.processKey});
						}
					});
				}
				},{
					text:'取消',
					addClass:'l-btn-red',iconCls:'icon-not',
					accessKey:{akey:18,mkey:67},
					handler:function(){
						$("#adddlg").dialog("close");
					}
				}]
			});
		$("#adddlg").dialog("open");
		}
	};
	
	module.exports={
		init:function(){
			var buttons ={ 
					title:{
						_class:"",
						text:"系统预设流程"
					},
					btn:[	
					     {
					    	 iconCls: 'icon-icon1_19',
					    	 addClass:'l-btn-display',
					    	 text:"一键导入",
					    	 url:'/view/oa/approval/sysProcess/add',
					    	 needRow:true,
					    	 handler:function(baseUrl,row){
					    	 	addDialogInit(baseUrl,row);
					    	 }
					     }
					     ]};
			datagridconfig = {
					fitColumns:true,
	                rownumbers:true,
					toolbar:buttons,
					columns:conf.columns,
					eid:"formviewdatagrid",
					pagination:false,
					singleSelect:true,
					args:{
						moudelType:CONST.ACT_MOUDEL_TYPE.APPROVAL,
						phoneStatus:'0'
						},
					check:true,
					url:BASEPATH+"/oa/act/process/querySysProcess.htm",
					pagination:true
			};
			/**/
			using(["datagrid","parser","droppable","draggable"],function(){
				dgutil = new EUtil.ajaxDataGrid(datagridconfig);
			});
		}
	}
});

